package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.CheckInApplyDto;
import com.zzyl.dto.ElderFamilyDto;
import com.zzyl.entity.*;
import com.zzyl.enums.CheckInStatusEnum;
import com.zzyl.mapper.*;
import com.zzyl.service.CheckInService;
import com.zzyl.service.ElderService;
import com.zzyl.vo.*;
import com.zzyl.vo.retreat.ElderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CheckInServiceImpl implements CheckInService {
    @Autowired
    private ElderService elderService;
    @Autowired
    private CheckInMapper checkInMapper;
    @Autowired
    private CheckInConfigMapper checkInConfigMapper;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private BedMapper bedMapper;


    /**
     * 分页查询
     *
     * @param elderName 老人姓名，模糊查询
     * @param idCardNo  身份证号，精确查询
     * @param pageNum   页码
     * @param pageSize  页面大小
     * @return 分页结果
     */
    @Override
    public PageBean<CheckInPageQueryVo> pageQuery(String elderName, String idCardNo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        //老人姓名，模糊查询，这里进行预处理
        elderName = CharSequenceUtil.isBlank(elderName) ? null : "%" + elderName + "%";

        //这里只查询入住状态的
        Page<CheckInPageQueryVo> pageResult = checkInMapper.selectByPage(elderName, idCardNo, CheckInStatusEnum.PROGRESSING.getOrdinal());
        return PageBean.of(pageResult, CheckInPageQueryVo.class);
    }

    /**
     * 入住详情
     *
     * @param id 入住id
     * @return 入住详情
     */
    @Override
    public CheckInDetailVo detail(Long id) {
        //入住信息
        CheckIn checkIn = checkInMapper.selectById(id);

        //老人信息
        ElderVo elderVo = elderService.selectByPrimaryKey(checkIn.getElderId());
        CheckInElderVo checkInElderVo = BeanUtil.toBean(elderVo, CheckInElderVo.class);
        checkInElderVo.setOneInchPhoto(elderVo.getImage());

        //入住配置信息
        CheckInConfig checkInConfig = checkInConfigMapper.findCurrentConfigByElderId(checkIn.getElderId());

        //合同信息
        Contract contract = contractMapper.selectByContractNo(checkIn.getRemark());

        //封装响应信息
        CheckInDetailVo checkInDetailVo = new CheckInDetailVo();
        checkInDetailVo.setCheckInElderVo(checkInElderVo);
        checkInDetailVo.setElderFamilyVoList(JSONUtil.toList(checkIn.getOtherApplyInfo(), ElderFamilyVo.class));
        checkInDetailVo.setCheckInConfigVo(BeanUtil.toBean(checkInConfig, CheckInConfigVo.class));
        checkInDetailVo.setCheckInContractVo(BeanUtil.toBean(contract, CheckInContractVo.class));
        return checkInDetailVo;
    }

    /**
     * 新增入住信息
     *
     * @param checkInApplyDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCheckIn(CheckInApplyDto checkInApplyDto) {
//        1.修改床位信息
        Long bedId = checkInApplyDto.getCheckInConfigDto().getBedId();
        Bed bed = bedMapper.getBedById(bedId);
        bed.setBedStatus(1);

//        2.新增入住老人表(后期可能优化，例如老人以前住过，数据库仍存在其数据，此时不用新增）
        Elder elder = BeanUtil.toBean(checkInApplyDto.getCheckInElderDto(), Elder.class);
        elder.setStatus(1);
        elder.setBedNumber(bed.getBedNumber());
        elder.setBedId(bedId);
        elder.setCreateBy(1671403256519078239L);
        elder.setUpdateTime(LocalDateTime.now());
        elder.setCreateBy(1671403256519078239L);
        elder.setUpdateTime(LocalDateTime.now());
        elderMapper.insert(elder);

//        3.新增入住老人子女信息
        Long addElderId = elder.getId();
        List<ElderFamilyDto> elderFamilyDtoList = checkInApplyDto.getElderFamilyDtoList();
        String  elderFamily = elderFamilyDtoList.toString();


//        4.新增入住表
        CheckIn checkIn = CheckIn.builder()
                .elderId(addElderId)
                .status(0)
                .title("随便取一个标题")
                .checkInCode("RZ202312281731052")
                .otherApplyInfo(elderFamily)
                .applicat("江奕云")
                .deptNo("100001006001000")
                .applicatId(1671403256519078243L)

                .checkInTime(LocalDateTime.now())
                .createBy(1671403256519078239L)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .updateBy(1671403256519078239L)
                .build();
        checkInMapper.insert(checkIn);


//        5.新增入住配置表
        CheckInConfig checkInConfig = BeanUtil.toBean(checkInApplyDto.getCheckInConfigDto(), CheckInConfig.class);
        checkInConfig.setElderId(addElderId);
        checkInConfig.setBedNumber(bed.getBedNumber());
        checkInConfig.setCheckInCode("RZ202312281731054");
        checkInConfig.setCreateTime(LocalDateTime.now());
        checkInConfig.setUpdateBy(1671403256519078239L);
        checkInConfig.setUpdateTime(LocalDateTime.now());
        checkInConfig.setCreateBy(1671403256519078239L);
        checkInConfigMapper.insert(checkInConfig);

//        6.新增用户合同表
        Contract contract = BeanUtil.toBean(checkInApplyDto.getCheckInContractDto(), Contract.class);
        contract.setElderId(addElderId);
        contract.setStatus(1);
        contract.setContractNo("HT202312281731051");
        contract.setElderName(elder.getName());

        contract.setCreateBy(1671403256519078239L);
        contract.setCreateTime(LocalDateTime.now());
        contract.setUpdateBy(1671403256519078239L);
        contract.setUpdateTime(LocalDateTime.now());

        contract.setStartTime(LocalDateTime.now());
        contract.setEndTime(LocalDateTime.now().plusYears(1));

        contractMapper.insert(contract);





    }
}
